from garden import db

class User(db.Model):
	__tablename__ = 't_user'
	__table_args__ = {"extend_existing": False,"comment":"用户表"}

	id = db.Column(db.Integer, primary_key=True)
	nickname = db.Column(db.String(50), unique=False,comment='昵称')
	loginname= db.Column(db.String(50), unique=True,comment='登陆名')
	passwd = db.Column(db.String(50), unique=False,comment='密码')
	email = db.Column(db.String(120), unique=False,comment='电邮')
	mobile = db.Column(db.String(20), unique=True,comment='手机')
	idno = db.Column(db.String(20), unique=True,comment='身份证')
	realname = db.Column(db.String(50), unique=False,comment='真实姓名')
	is_valid = db.Column(db.Boolean,unique=False, comment='是否有效')
	roles = db.Column(db.String(20), unique=False,comment='角色')
	create_time = db.Column(db.DateTime,nullable=False,comment='创建时间')
	modify_time = db.Column(db.DateTime,nullable=True,comment='修改时间')
	status = db.Column(db.SmallInteger, nullable=False,comment='用户状态 1:正常 ,0:注销, 2:异常')
	openid = db.Column(db.String(100), unique=True,comment='微信openid')
	avatar = db.Column(db.String(500), unique=False,comment='头像')
	cmnt_id =  db.Column(db.Integer,db.ForeignKey('t_community.id'),nullable=True,default=1000, comment='所属小区')
	community = db.relationship('Community',backref=db.backref('t_user', lazy=True))
	number =  db.Column(db.String(100),nullable=True,comment='门牌号')

	
	def __init__(self, nickname=None, loginname=None, passwd=None, email=None, mobile=None, idno=None, realname=None, is_valid=None, roles=None, \
		create_time=None, modify_time=None, status=None, openid=None, avatar=None, cmnt_id=None, number=None):

		self.nickname = nickname
		self.loginname = loginname
		self.passwd = passwd
		self.email = email
		self.mobile = mobile
		self.idno = idno
		self.realname = realname
		self.is_valid = is_valid
		self.roles = roles
		self.create_time = create_time
		self.modify_time = modify_time
		self.status = status
		self.openid = openid
		self.avatar = avatar
		self.cmnt_id = cmnt_id
		self.number = number

	def __repr__(self):
		return '<User %r>' % (self.nickname)

class Role(db.Model):
	__tablename__ = 't_role'
	__table_args__ = {"extend_existing": False,"comment":"角色表"}

	id = db.Column(db.Integer, primary_key=True, autoincrement=False, default=1001, comment='1001:居民,1002:物业,1003:物业管理员,1000:超级管理员')
	rolename = db.Column(db.String(50), unique=False,comment='角色名称')

	def __init__(self, rolename=None):
		self.rolename = rolename

	def __repr__(self):
		return '<Role %r>' % (self.rolename)


class Community(db.Model):
	__tablename__ = 't_community'
	__table_args__ = {"extend_existing": False,"comment":"小区表"}

	id = db.Column(db.Integer, primary_key=True)
	cmnt_name = db.Column(db.String(50), unique=False,comment='小区名称')
	address = db.Column(db.String(500), unique=False,comment='地址')
	city = db.Column(db.String(50), unique=False,comment='城市')
	district = db.Column(db.String(50), unique=False,comment='区县')
	area_code = db.Column(db.String(50), unique=False,comment='地区编码')
	gps=db.Column(db.String(200), unique=False,comment='gps坐标')
	developer=db.Column(db.String(50), unique=False,comment='开发商')
	property_com=db.Column(db.String(50), unique=False,comment='物业公司')
	create_time = db.Column(db.DateTime,nullable=False,comment='创建时间')
	modify_time = db.Column(db.DateTime,nullable=False,comment='修改时间')
	is_visible = db.Column(db.Boolean, nullable=False,default=True, comment='是否显示 True:不显示, False:显示')

	def __init__(self, cmnt_name=None, address=None, city=None, district=None, area_code=None, gps=None, \
		developer=None, property_com=None, create_time=None, modify_time=None):

		self.cmnt_name = cmnt_name
		self.address = address
		self.city = city
		self.district = district
		self.area_code = area_code
		self.gps = gps
		self.developer = developer
		self.property_com = property_com
		self.create_time = create_time
		self.modify_time = modify_time

	def __repr__(self):
		return '<Community %r>' % (self.cmnt_name)

class Topic(db.Model):
	__tablename__ = 't_topic'
	__table_args__ = {"extend_existing": False,"comment":"小区话题表"}

	id = db.Column(db.Integer, primary_key=True)
	title = db.Column(db.String(256),nullable=False,comment='话题标题')
	content = db.Column(db.Text,nullable=False,comment='话题内容')
	category = db.Column(db.String(2),nullable=False,comment='类别 00:讨论,01:公告')
	tags = db.Column(db.String(128),nullable=True,comment='标签')
	publisher = db.Column(db.Integer,db.ForeignKey('t_user.id'), nullable=False,comment='发布人')
	user = db.relationship('User',backref=db.backref('t_topic', lazy=True))
	pub_time = db.Column(db.DateTime,nullable=False,comment='发布时间')
	cmnt_id =  db.Column(db.Integer,nullable=False,comment='所属小区')
	replies = db.relationship('TopicReply', backref='t_topic', lazy=True)
	del_flag = db.Column(db.SmallInteger, nullable=False,default=0, comment='删除标记 0:未删除, 1:已删除')

	def __init__(self, title=None, content=None, category=None, tags=None, publisher=None, pub_time=None, cmnt_id=None):
		self.title = title
		self.content = content
		self.category = category
		self.tags = tags
		self.publisher = publisher
		self.pub_time = pub_time
		self.cmnt_id = cmnt_id

	def __repr__(self):
		return '<Topic %r>' % (self.title)

class TopicReply(db.Model):
	__tablename__ = 't_topic_reply'
	__table_args__ = {"extend_existing": False,"comment":"话题回复表"}

	id = db.Column(db.Integer, primary_key=True)
	content = db.Column(db.Text,nullable=False,comment='回复内容')
	tpc_id = db.Column(db.Integer, db.ForeignKey('t_topic.id'),nullable=False,comment='所属话题')
	publisher = db.Column(db.Integer,db.ForeignKey('t_user.id'), nullable=False,comment='发布人')
	user = db.relationship('User',backref=db.backref('t_topic_reply', lazy=True))
	pub_time = db.Column(db.DateTime,nullable=False,comment='发布时间')

	def __init__(self, content=None, tpc_id=None, publisher=None, pub_time=None):
		self.content = content
		self.tpc_id = tpc_id
		self.publisher = publisher
		self.pub_time = pub_time

	def __repr__(self):
		return '<TopicReply %r>' % (self.content)

class Repair(db.Model):
	__tablename__ = 't_repair'
	__table_args__ = {"extend_existing": False,"comment":"报修表"}

	id = db.Column(db.Integer, primary_key=True)
	title = db.Column(db.String(256),nullable=False,comment='标题')
	content = db.Column(db.Text,nullable=False,comment='内容')
	publisher_id = db.Column(db.Integer,db.ForeignKey('t_user.id'), nullable=False,comment='发布人')
	publisher = db.relationship('User',backref=db.backref('t_repair', lazy=True), foreign_keys=[publisher_id])
	pub_time = db.Column(db.DateTime,nullable=False,comment='发布时间')
	cmnt_id =  db.Column(db.Integer,nullable=False,comment='所属小区')
	replies = db.relationship('RepairReply', backref='t_repair', lazy=True)
	status = db.Column(db.String(2),nullable=False,comment='处理状态 00:待确认,01:已确认, 02:处理中,03:处理完成,04:已解决')
	urgency = db.Column(db.SmallInteger, nullable=False,comment='紧急程度 0:低 ,1:中, 2:高')
	operator_id = db.Column(db.Integer,db.ForeignKey('t_user.id'),nullable=False,comment='经办人')
	operator = db.relationship('User',backref=db.backref('t_repair_op', lazy=True), foreign_keys=[operator_id])
	modify_time = db.Column(db.DateTime,nullable=False,comment='修改时间')
	logs = db.relationship('RepairLog', backref='t_repair', lazy=True)
	del_flag = db.Column(db.SmallInteger, nullable=False,default=0, comment='删除标记 0:未删除, 1:已删除')

	def __init__(self, title=None ,content=None ,publisher_id=None ,pub_time=None ,cmnt_id=None ,status=None ,urgency=None ,operator_id=None ,modify_time =None):
		self.title = title
		self.content = content
		self.publisher_id = publisher_id
		self.pub_time = pub_time
		self.cmnt_id = cmnt_id
		self.status = status
		self.urgency = urgency
		self.operator_id = operator_id
		self.modify_time = modify_time

	def __repr__(self):
		return '<Repair %r>' % (self.title)

class RepairLog(db.Model):
	__tablename__ = 't_repair_log'
	__table_args__ = {"extend_existing": False,"comment":"报修日志表"}

	id = db.Column(db.Integer, primary_key=True)
	content = db.Column(db.Text,nullable=False,comment='回复内容')
	rpr_id = db.Column(db.Integer, db.ForeignKey('t_repair.id'),nullable=False,comment='所属报修单')
	changer_id = db.Column(db.Integer,db.ForeignKey('t_user.id'),nullable=False,comment='修改人')
	changer = db.relationship('User',backref=db.backref('t_repair_log', lazy=True))
	pub_time = db.Column(db.DateTime,nullable=False,comment='发布时间')

	def __init__(self, content=None ,rpr_id=None ,changer_id=None ,pub_time=None):
		self.content = content
		self.rpr_id = rpr_id
		self.changer_id = changer_id
		self.pub_time = pub_time

	def __repr__(self):
		return '<RepairLog %r>' % (self.content)


class RepairReply(db.Model):
	__tablename__ = 't_repair_reply'
	__table_args__ = {"extend_existing": False,"comment":"报修回复表"}

	id = db.Column(db.Integer, primary_key=True)
	content = db.Column(db.Text,nullable=False,comment='回复内容')
	rpr_id = db.Column(db.Integer, db.ForeignKey('t_repair.id'),nullable=False,comment='所属报修单')
	publisher_id = db.Column(db.Integer,db.ForeignKey('t_user.id'),nullable=False,comment='发布人')
	publisher = db.relationship('User',backref=db.backref('t_repair_reply', lazy=True))
	pub_time = db.Column(db.DateTime,nullable=False,comment='发布时间')

	def __init__(self, content=None, rpr_id=None, publisher_id=None, pub_time=None):
		self.content = content
		self.rpr_id = rpr_id
		self.publisher_id = publisher_id
		self.pub_time = pub_time

	def __repr__(self):
		return '<RepairReply %r>' % (self.content)